package forum.command;

import forum.exceptions.CreateExistingEntityException;
import forum.services.UserService;

public class RegistrationCommand implements Command {

    private String email;
    private String nickName;
    private String password;

    private UserService userService = new UserService();

    public void setParameters(String... args) {
        if (args.length != 3) {
            throw new IllegalArgumentException ("Incorrect number of arguments");
        }
        email = args[0];
        nickName = args[1];
        password = args[2];
    }

    @Override
    public void setContext(CommandContext context) {
        if (context.getUser() != null) {
            throw new IllegalArgumentException("You already registered");
        }
    }

    @Override
    public void updateContext(CommandContext context) {
    }

    @Override
    public void execute() {
        try {
            userService.register(email, nickName, password);
        } catch (CreateExistingEntityException e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public String help() {
        return "Type 'register | %email% | %nickname% | %password%' to sign up";
    }
}
